<template>
  <div class='container'>
    <div class="app-container">
      <el-card>
        <el-row >
        <el-col :span="20">
          <el-form label-width="80px" class="inline-block" :inline="true">
            <el-form-item label="学科名称" >
              <el-input style="width:200px" size="small" v-model="reqParams.subjectName" placeholder="请输入学科名称"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="eliminate">清除</el-button>
              <el-button size="small" type="primary" @click="getsimple">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="4" style="text-align:right">
          <el-button type="success" size="small" icon="el-icon-edit" @click="isShowsubjectsAdd">新增学科</el-button>
        </el-col>
      </el-row>
      <!-- 警告条 -->
      <el-alert
        :title="`数据一共${total}条`"
        type="info"
        :closable="false"
        show-icon>
      </el-alert>
      <!-- table表格 -->
      <el-table :data="tableLIst" style="width: 100%">
        <el-table-column prop="date" label="序号" width="80" type="index">
        </el-table-column>
        <el-table-column prop="subjectName" width="100px" label="学科名称">
        </el-table-column>
        <el-table-column prop="username" width="100px" label="创建者">
        </el-table-column>
         <el-table-column prop="addDate" label="创建日期">
           <template #default="{ row }">{{ row.addDate | parseTimeByString}}</template>
        </el-table-column>
         <el-table-column label="前台是否显示">
           <template #default="{ row }">{{+row.isFrontDisplay === 1 ? '是' : '否'}}</template>
        </el-table-column>
         <el-table-column prop="twoLevelDirectory" width="120px" label="二级目录">
        </el-table-column>
         <el-table-column prop="tags" width="90px" label="标签">
        </el-table-column>
         <el-table-column prop="totals" width="90px" label="题目数量">
        </el-table-column>
         <el-table-column width="240px" label="操作">
            <template #default="{ row }">
              <el-button type="text" @click="$router.push(`directorys?id=${row.id}&name=${row.subjectName}`)">学科分类</el-button>
              <el-button type="text" @click="$router.push(`tags?id=${row.id}&name=${row.subjectName}`)">学科标签</el-button>
              <el-button type="text" @click="modifySub(row)">修改</el-button>
              <el-button type="text" @click="delSub(row)">删除</el-button>
            </template>
        </el-table-column>
      </el-table>
      <!--  分页功能  -->
      <el-pagination
        class="subjects-pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="reqParams.page"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="reqParams.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      <!-- 新增学科弹框 -->
      <subjectsAdd ref="subjectsAddRef" :dataLIst="dataLIst" @getSubjectList="getSubjectList"></subjectsAdd>
      </el-card>
    </div>
  </div>
</template>

<script>
import { list, remove } from '@/api/hmmm/subjects.js'
import { parseTime } from '@/filters/index.js'
import subjectsAdd from '../components/subjects-add.vue'
export default {
  components: { subjectsAdd },
  name: 'Subjects',
  data () {
    return {
      loading: false,
      dataLIst: {},
      tableLIst: [], // 学科数据列表
      reqParams: {
        subjectName: '', // 学科名称
        page: 1, // 当前页数
        pagesize: 10 // 页尺寸
      },
      total: 0,
      parseTime // 时间筛选
    }
  },
  created () {
    // 获取学科数据列表
    this.getSubjectList()
  },
  methods: {
    // 获取学科数据列表
    async getSubjectList () {
      const { data } = await list(this.reqParams)
      this.tableLIst = data.items
      this.total = data.counts
    },
    handleSizeChange (pagesize) { // 每页多少条
      this.reqParams.pagesize = pagesize
      this.getSubjectList()
    },
    handleCurrentChange (page) { // 当前多少页
      this.reqParams.page = page
      this.getSubjectList()
    },
    // 点击清除按钮，清除input框的数据
    eliminate () {
      this.reqParams.subjectName = ''
    },
    // 筛选按钮
    getsimple () {
      this.reqParams.page = 1
      this.getSubjectList()
    },
    // 打开弹窗
    isShowsubjectsAdd () {
      this.$refs.subjectsAddRef.title = '新增学科'
      this.dataLIst = {}
      this.$nextTick(() => {
        this.$refs.subjectsAddRef.isShowdialog()
      })
    },
    // 删除按钮
    async delSub (row) {
      const res = await this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        type: 'warning'
      }).catch(() => {})
      if (res !== 'confirm') return this.$message.info('已取消该操作')
      await remove(row)
      this.$message.success('删除成功')
      if (this.tableLIst.length === 1 && this.reqParams.page > 1) {
        this.reqParams.page--
      }
      this.getSubjectList()
    },
    // 修改按钮
    async modifySub (row) {
      this.$refs.subjectsAddRef.title = '修改学科'
      this.dataLIst = row
      this.$nextTick(() => {
        this.$refs.subjectsAddRef.isShowdialog()
      })
    }
  }
  // computed: {
  //   isFrontDisplay () {
  //     return this.tableLIst.isFrontDisplay === 1 ? '否' : '是'
  //   }
  // }
}
</script>

<style scoped lang="scss">
.inline-block {
  display: inline-block;
}
.subjects-pagination {
  float: right;
  margin: 20px;
}

</style>
